#include "support_common.h" /* include peripheral declarations and more */
#include <stdio.h>
#include "uc_dipsw.h"
#include "uc_led.h"
#include "dtim.h"
#include "gpio.h"

int sw2;

static int dipsw_delay_poll()
{
	int sw3 = gpio_port_dd_get_pin_state(6);
	int sw4 = gpio_port_dd_get_pin_state(7);
	
	if(sw3 == 1 & sw4 == 1)
	{
		return 250;
	}
	else if(sw3 == 1 & sw4 == 0)
	{
		return 350;
	}
	else if(sw3 == 0 & sw4 == 1)
		{
			return 450;
		}
	else if(sw3 == 0 & sw4 == 0)
		{
			return 550;
		}
	return 0;
}
	
static void dipsw_onoff_poll()
{
	if (gpio_port_dd_get_pin_state(5) == 1)
	{
		sw2 = 0;
	}
	else if (gpio_port_dd_get_pin_state(5) == 0)
	{
		sw2 = 1;
	}
}
	
static void flash_led_sequence(int p_delay)
{ 
	uc_led_all_off();
	p_delay = (1000*dipsw_delay_poll());
	dtim0_delay(p_delay);
	dipsw_onoff_poll();
	int i= -1;
	
	while(1)
	{
		while(sw2 == 1 && i<=4)
		{
			i++;
			if (i>= 4)
				i = 0;
			uc_led_on(i);
			dtim0_delay(p_delay);
			uc_led_off(i);
			dtim0_delay(p_delay);
			dipsw_onoff_poll();
			p_delay = (1000*dipsw_delay_poll());
		}
		while (sw2 == 0 && i>= -1)
		{
			i--;
			if (i<=-1)
				i = 3;
			uc_led_on(i);
			dtim0_delay(p_delay);
			uc_led_off(i);
			dtim0_delay(p_delay);
			dipsw_onoff_poll();
			p_delay = (1000*dipsw_delay_poll());			
		}
	}

}
	
static void init()
{
		uc_dipsw_init();
		uc_led_init();
		dtim0_init();
}

int main()
{
	init();
	flash_led_sequence(250000);
	
}

